<?php

namespace app\admin\controller;

use think\Db;
use think\Controller;
use think\Request;
use think\Session;


class User extends AdminController
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        // 读取用户列表
        $data = Db::table('sm_h_user')
            ->order('sm_h_user.id')
            ->paginate(10);
        $page = $data->render();
        return view('admin@User/index', [
            'data' => $data,
            'title' => '我是显示用户页面',
            'page' => $page
        ]);
    }

    public function add()
    {
        return view('admin@Auser/add', [
            'title' => '我是添加管理员页面'
        ]);
    }

    // 添加管理员
    public function insert(Request $request)
    {
        $data = $request->post();
        // var_dump($data);die;
        $d['UserName'] = $data['UserName'];
        $d['Name'] = $data['Name'];
        $d['Password'] = md5($data['Password']);

        $s = [
            'username' => $d['UserName'],
            'name' => $d['Name'],
            'userpass' => $d['Password']
        ];

        $result = Db('a_user')->insert($s);

        $uid = Db::name('a_user')->field('id')->where('name', $d['Name'])->find();
        $d = [
            'uid' => $uid['id'],
            'rid' => '7',
        ];
        $list = db('a_user_role')->insert($d);
        if ($result) {
            return $this->success('新增成功');
        } else {
            return $this->error('新增失败');
        }
    }

    public function delete($id)
    {
        $result = db('h_user')->where('id', $id)->delete();
        // dump($result);die;
        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
        } else {
            $info['status'] = false;
            $info['id'] = $id;
        }
        return json($info);
    }

    public function edit($id)
    {
        $data = db('h_user')->find($id);
        $imgs = $data['face'];
        return view('admin@User/edit', [
            'title' => '我是修改ID:' . $id . '的用户页面',
            'data' => $data,
            'id' => $id,
            'imgs' => $imgs
        ]);
    }

    public function update(Request $request)
    {
        $data = $request->post();
        $id = $data['id'];
        if ($data['password'] == '') {
            unset($data['password']);
            $a = [
                'status' => $data['go'],
            ];
        } else {
            $data['password'] = md5($data['password']);
            $a = [
                'password' => $data['password'],
                'status' => $data['go'],
            ];
        }

        $result = Db::name('h_user')->where('id', $id)->update($a);

        if ($result) {
            return $this->success('编辑成功', 'User/index');
        } else {
            return $this->error('编辑失败');
        }
    }


    function search()
    {
        $where = [];
        $keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';
        // var_dump($keyword);die;
        if ($keyword) {
            $where['name'] = ['like', '%' . $keyword . '%'];
            $list = Db::name('h_user')->where($where)->order('id DESC')->select();
            if ($list) {
                return view('admin@User/search', [
                'title' => '搜索关键字:' . $keyword,
                'list' => $list,
        ]);
            }else{
                return view('admin@User/search', [
            'title' => '搜索关键字:' . $keyword,
            'res' => '没有找到关于 [ '.$keyword.' ] 的用户，换个搜索词试试吧。',
            ]);
            }
            
        }else{
            return view('admin@User/search', [
            'title' => '搜索关键字:' . $keyword . '页面',
            'res' => '没有找到关于 [  ] 的用户，换个搜索词试试吧。',
        ]);
        }
       
        
    }

}